package cn.tedu._05notice.controller;

import cn.tedu._05notice.base.response.JsonResult;
import cn.tedu._05notice.base.response.StatusCode;
import cn.tedu._05notice.mapper.NoticeMapper;
import cn.tedu._05notice.pojo.dto.NoticeAddParam;
import cn.tedu._05notice.pojo.dto.NoticeListQuery;
import cn.tedu._05notice.pojo.dto.NoticeUpdateParam;
import cn.tedu._05notice.pojo.entity.Notice;
import cn.tedu._05notice.pojo.vo.NoticeDetailInfoVO;
import cn.tedu._05notice.pojo.vo.NoticeListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;


@Slf4j  //Lombox提供的日志注解
@RestController
@Tag(name="资讯管理模块")
@RequestMapping("v1/notices/")
public class NoticeController {
    @Autowired
    private NoticeMapper noticeMapper;


    @PostMapping("add")
    @Operation(summary="添加资讯")
    @ApiOperationSupport(order = 10)

    public JsonResult addNotice(NoticeAddParam noticeAddParam){
        log.debug("noticeAddParam="+noticeAddParam);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeAddParam,notice);
        notice.setUserId(1L);
        notice.setCreatedTime(new Date());
        notice.setUpdateTime(new Date());

        log.debug("notice="+notice);
        noticeMapper.insertNotice(notice);
        return JsonResult.ok();
    }
    @GetMapping("list")
    @Operation(summary = "资讯列表")
    @ApiOperationSupport(order = 20)

    public JsonResult listNotice(NoticeListQuery noticeListQuery){
        log.debug("noticeListQuery="+noticeListQuery);
        List<NoticeListVO> list = noticeMapper.listNotice(noticeListQuery);
        return JsonResult.ok(list);
    }

    @Parameters(value = {    @Parameter(name="id",description="资讯编号",required = true),
            @Parameter(name="username",description = "用户名称",required = true,hidden = true)})
    @GetMapping("detail")
    @Operation(summary = "资讯详情")
    @ApiOperationSupport(order = 30)
    public JsonResult detailNotice(Long id,String username){
        log.debug("id="+id);
        List<NoticeDetailInfoVO> list1 = noticeMapper.listDetailNotice(id);
        return JsonResult.ok(list1);
    }
    @DeleteMapping("del")
    @Operation(summary = "删除资讯")
    @ApiOperationSupport(order = 40)

    public JsonResult deleteNotice(Long id){
        log.debug("id="+id);
        noticeMapper.listDeleteNotice(id);
        return JsonResult.ok();
    }

    @PutMapping("update")
    @Operation(summary = "更新资讯")
    @ApiOperationSupport(order = 50)
    @Parameters(value = {
            @Parameter(name="id"),
            @Parameter(name="title"),
            @Parameter(name="content"),
            @Parameter(name="type"),
            @Parameter(name="status"),
            @Parameter(name="noticeAddParam",hidden = true)
    })

    public JsonResult updateNotice(NoticeUpdateParam noticeUpdateParam){
        log.debug("noticeUpdateParam="+noticeUpdateParam);
        Notice notice=new Notice();
        BeanUtils.copyProperties(noticeUpdateParam, notice);
        notice.setUpdateTime(new Date());
        log.debug("notice="+notice);
        int rows=noticeMapper.updateNoticeById(notice);
        if (rows > 0){
            return JsonResult.ok();
        }else{
            return new JsonResult(StatusCode.OPERATION_FAILED,"修改失败，不存在");
        }
    }


}
